package com.pktechan.manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.manage.entity.WxMp;
import com.pktechan.manage.service.WxMpService;
import com.pktechan.manage.utils.HtmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 公众号配置相关功能
 * @author: s.zhangbj03307
 * @create: 2021/3/9 11:14
 **/
@Controller
@RequestMapping("wxMpController")
public class WxMpController {
    private static final Logger logger = LoggerFactory.getLogger(WxMpController.class);

    @Autowired
    private WxMpService wxMpService;

    /**
     * 根据Id查询公众号配置表
     *
     * @param wxMpId
     * @param response
     */
    @RequestMapping(value = "getWxMpById", method = RequestMethod.POST)
    public void getWxMpById(@RequestParam Integer wxMpId, HttpServletResponse response) {
        try {
            logger.info("根据Id=" + wxMpId + "查询公众号配置表");
            WxMp wxMp = wxMpService.getById(wxMpId);
            HtmlUtil.writerSuccessJson(response, wxMp);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 查询公众号配置表列表
     *
     * @param response
     */
    @RequestMapping(value = "getWxMpList", method = RequestMethod.POST)
    public void getWxMpList(@RequestBody WxMp wxMp, HttpServletResponse response) {
        try {
            logger.info("查询公众号配置表列表");
            PageHelper.startPage(wxMp.getPageNo(), wxMp.getPageSize());
            List<WxMp> wxMpList = wxMpService.getList(wxMp);
            PageInfo pageInfo = new PageInfo(wxMpList);
            HtmlUtil.writerSuccessJson(response, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 添加到公众号配置表
     *
     * @param response
     */
    @RequestMapping(value = "saveWxMp", method = RequestMethod.POST)
    public void saveWxMp(@RequestBody WxMp wxMp, HttpServletResponse response) {
        try {
            logger.info("添加到公众号配置表");
            wxMpService.save(wxMp);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id修改公众号配置表
     *
     * @param response
     */
    @RequestMapping(value = "updateWxMp", method = RequestMethod.POST)
    public void updateWxMp(@RequestBody WxMp wxMp, HttpServletResponse response) {
        try {
            logger.info("根据Id" + wxMp.getId() + "修改公众号配置表");
            wxMpService.update(wxMp);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }

    /**
     * 根据Id删除公众号配置
     *
     * @param wxMpId
     * @param response
     */
    @RequestMapping(value = "deleteWxMpById", method = RequestMethod.POST)
    public void deleteWxMpById(@RequestParam Integer wxMpId, HttpServletResponse response) {
        try {
            logger.info("根据Id=" + wxMpId + "删除公众号配置");
            wxMpService.deleteById(wxMpId);
            HtmlUtil.writerSuccessJson(response);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response, e.getMessage());
        }
    }
}
